import requset from "/utils/request";
import clock from "/api/clock/index"
Page({
  data: {
    userObjInfo: {},
    form: {
      pageNumber: 1,
      pageSize: 10
    },
    dateUnit: "",
    list: [],
    totalNum: 0,
  },
  onLoad(option) {
    // console.log(JSON.parse(option.form),"JSON.parse(option.form),")
    const form = JSON.parse(option.form)
    this.setData({
      userObjInfo: JSON.parse(option.item),
      "form.dingUserId": JSON.parse(option.form).dingUserId ? form.dingUserId: JSON.parse(option.item).userInfo.dingUserId,
      "form.merchantName": form.merchantName,
      "form.createTimeFrom":form.createTimeFrom,
      "form.createTimeTo": form.createTimeTo,
      dateUnit: option.dateUnit
    })
    this.queryList(this.data.form)
  },
  // 处理下拉刷新函数
  onPullDownRefresh() {
    setTimeout(() => {
      this.setData({
        "form.pageNumber": 1
      })
      this.queryList(this.data.form).then(() => {
        dd.stopPullDownRefresh(); //停止下拉刷新
      })
    }, 1000)
  },
  // 页面被拉到底部
  onReachBottom() {
    this.setData({
      'form.pageNumber': ++this.data.form.pageNumber
    })
    this.data.list.length === this.data.totalNum ? '' : this.queryList(this.data.form)
  },
  queryList(option) {
    return new Promise((resolve,reject) =>{
      dd.showLoading({
        content: "加载中..."
      })
      requset({
        url: clock.getVisitRecordList,
        data: option
      }).then((res) => {
          this.setData({
            list: this.data.form.pageNumber === 1 ? res.voList : [...this.data.list, ...res.voList], // 分页
            totalNum: res.totalNum,
          })
          dd.hideLoading();
          resolve()
      })
    })
  }
});
